package main

import (
	"fmt"
	"os"
	"os/exec"
	"time"
)

func Exists(path string) bool {
	_, err := os.Stat(path) //os.Stat获取文件信息
	if err != nil {
		if os.IsExist(err) {
			return true
		}
		return false
	}
	return true
}

func TimerPull() {
	//os.Executable()
	filePath := "api_markdown_files"
	//filePath := "view"
	if Exists(filePath) {
		os.Chdir(filePath)
		//dir, err := filepath.Abs(filepath.Dir("./"))
		//fmt.Println(string(dir), err)
		ls := exec.Command("git", "pull")
		out, err := ls.Output()
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(string(out))
		// 阻塞
		ls.Run()
		// 不阻塞
		//ls.Start()
		//ls.Wait()
		os.Chdir("../")
	} else {
		fmt.Println(fmt.Sprintf("Path:%s Not Exist!", filePath))
	}

}

func main() {
	//for {
	//	TimerPull()
	//	time.Sleep(time.Duration(1) * time.Second)
	//}
	ticker := time.NewTicker(1 * time.Second)
	for timeNow := range ticker.C {
		fmt.Println(timeNow, "hhh")
		TimerPull()
	}
}
